package com.mybatis.util;

import org.apache.ibatis.binding.MapperMethod;
import org.springframework.data.domain.Pageable;

import java.util.Map;

public class ClassUtil {

    public static <T> T findObjectFromParams(Object parameter, Class<T> pageableClass) {
        if (parameter==null)
        {
            return null;
        }
        if (pageableClass.isAssignableFrom(parameter.getClass()))
        {
            return (T)parameter;
        }
        if (parameter instanceof MapperMethod.ParamMap)
        {
            MapperMethod.ParamMap<Object> map =(MapperMethod.ParamMap<Object>)parameter;
            for (Map.Entry<String,Object> entry:map.entrySet())
            {
                Object value = entry.getValue();
                if (null!= value && pageableClass.isAssignableFrom(value.getClass()))
                {
                    return (T)value;
                }
            }
        }
        return null;
    }
}
